package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patrol.domain.PtNotificationUser;
import com.ruoyi.patrol.domain.vo.PtNotificationUserVo;
import com.ruoyi.patrol.service.IPtNotificationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知用户Controller
 *
 * @author 理想三旬
 * @date 2022-11-08
 */
@RestController
@RequestMapping("/patrol/notifination_user")
public class PtNotificationUserController extends BaseController {
    @Autowired
    private IPtNotificationUserService ptNotificationUserService;

    /**
     * 查询通知用户列表
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:list')")
     */
    @GetMapping("/list")
    public TableDataInfo list(PtNotificationUser ptNotificationUser) {
        startPage();
        List<PtNotificationUser> list = ptNotificationUserService.selectPtNotificationUserList(ptNotificationUser);
        return getDataTable(list);
    }

    /**
     * 根据通知id查询通知用户列表
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:list')")
     */
    @GetMapping(value = "/admin/list")
    public TableDataInfo list(PtNotificationUserVo ptNotificationUserVo) {
        startPage();
        List<PtNotificationUserVo> list = ptNotificationUserService.selectPtNotificationUserByNotifyId(ptNotificationUserVo);
        return getDataTable(list);
    }

    /**
     * 根据通知id查询已读条数
     */
    @GetMapping(value = "/read")
    public int list(String notificationId) {
        return ptNotificationUserService.selectCancelCondition(notificationId);
    }

    /**
     * 学校管理员查看通知列表
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:list')")
     */
    @GetMapping(value = "/staff/list")
    public TableDataInfo staffList(PtNotificationUserVo ptNotificationUserVo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user != null) {
            ptNotificationUserVo.setUserId(user.getUserId());
        }
        startPage();
        List<PtNotificationUserVo> list = ptNotificationUserService.selectPtNotificationUserByUserId(ptNotificationUserVo);
        return getDataTable(list);
    }

    //    @PreAuthorize("@ss.hasPermi('patrol:notifination_user:list')")
    @GetMapping(value = "/staff/list/nopage")
    public List<PtNotificationUserVo> staffListNopage(PtNotificationUserVo ptNotificationUserVo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user != null) {
            ptNotificationUserVo.setUserId(user.getUserId());
        }
        List<PtNotificationUserVo> list = ptNotificationUserService.selectPtNotificationUserByUserId(ptNotificationUserVo);
        return list;
    }

    /**
     * 查询通知中的学校id
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:list')")
     */
    @GetMapping(value = "/admin/school/{notificationId}")
    public List<Long> schoolIdList(@PathVariable("notificationId") String notificationId) {
        return ptNotificationUserService.selectPtNotificationUserSchoolId(notificationId);
    }

    /**
     * 导出通知用户列表
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:export')")
     */
    @Log(title = "通知用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtNotificationUser ptNotificationUser) {
        List<PtNotificationUser> list = ptNotificationUserService.selectPtNotificationUserList(ptNotificationUser);
        ExcelUtil<PtNotificationUser> util = new ExcelUtil<PtNotificationUser>(PtNotificationUser.class);
        return util.exportExcel(list, "notifination_user");
    }

    /**
     * 获取通知用户详细信息
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:query')")
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ptNotificationUserService.selectPtNotificationUserById(id));
    }

    /**
     * 新增通知用户
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:add')")
     */
    @Log(title = "通知用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtNotificationUser ptNotificationUser) {
        return toAjax(ptNotificationUserService.insertPtNotificationUser(ptNotificationUser));
    }

    /**
     * 修改通知用户
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:edit')")
     */
    @Log(title = "通知用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtNotificationUser ptNotificationUser) {
        return toAjax(ptNotificationUserService.updatePtNotificationUser(ptNotificationUser));
    }

    /**
     * 根据通知id删除通知用户
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:remove')")
     */
    @Log(title = "通知用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/notify/{notificationId}")
    public AjaxResult remove(@PathVariable String notificationId) {
        return toAjax(ptNotificationUserService.deletePtNotificationUserById(notificationId));
    }

    /**
     * 删除通知用户
     *
     * @PreAuthorize("@ss.hasPermi('patrol:notifination_user:remove')")
     */
    @Log(title = "通知用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ptNotificationUserService.deletePtNotificationUserByIds(ids));
    }
}
